package com.example.g5project.tools;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author Hu Zirui
 * @version 1.0.0
 * @ClassName EntityUtils.java
 * @Description TODO
 * @createTime 2022年03月26日 22:56:00
 */
public class EntityUtils {
    private static String[] getNullPropertyNames(Object source) {
        Class<?> clz = source.getClass();
        Field[] fields = clz.getDeclaredFields();
        List<String> propertyNames = new ArrayList<>();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                if (null == field.get(source))
                    propertyNames.add(field.getName());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return propertyNames.toArray(new String[]{});
    }

    public static <T> T copyProperties(T src, T target, boolean ignoreNull) {
        if (ignoreNull) {
            BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
        } else {
            BeanUtils.copyProperties(src, target);
        }
        return target;
    }

    public static String DateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public String transformDate(String time1, String time2) {
        String[] hold_time_array1 = time1.split(":");
        Integer hours1 = Integer.valueOf(hold_time_array1[0]);
        Integer minutes1 = Integer.valueOf(hold_time_array1[1]);
        Integer seconds1 = Integer.valueOf(hold_time_array1[2]);
        String[] hold_time_array2 = time2.split(":");
        Integer hours2 = Integer.valueOf(hold_time_array2[0]);
        Integer minutes2 = Integer.valueOf(hold_time_array2[1]);
        Integer seconds2 = Integer.valueOf(hold_time_array2[2]);
        int hours = hours1 + hours2;
        int minutes = minutes1 + minutes2;
        int seconds = seconds1 + seconds2;
        long s2m = seconds / 60;
        seconds %= 60;
        minutes += s2m;
        long m2h = minutes / 60;
        minutes %= 60;
        hours += m2h;
        String diffSecondsStr = seconds < 10 ? "0" + seconds : "" + seconds;
        String diffMinutesStr = minutes < 10 ? "0" + minutes : "" + minutes;
        String diffHoursStr = hours < 10 ? "0" + hours : "" + hours;
        String hold_time = diffHoursStr + ":" + diffMinutesStr + ":" + diffSecondsStr;
        return diffHoursStr + ":" + diffMinutesStr + ":" + diffSecondsStr;
    }

    public  String TimeStamp2Date(String timestampString) {
        String formats = "yyyy-MM-dd HH:mm:ss";
        long timestamp = Long.parseLong(timestampString) * 1000;
        return new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
    }

}
